package com.beyond.trace.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.beyond.trace.domain.TraceLand;
import com.ruoyi.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import javax.annotation.Resource;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import com.beyond.trace.mapper.TracePlantRecordMapper;
import com.beyond.trace.domain.TracePlantRecord;
import com.beyond.trace.service.ITracePlantRecordService;

/**
 * 农事记录信息Service业务层处理
 * 
 * @author yao
 * @date 2022-03-02
 */
@Service
public class TracePlantRecordServiceImpl extends ServiceImpl<TracePlantRecordMapper, TracePlantRecord> implements ITracePlantRecordService
{
    @Resource
    private TracePlantRecordMapper tracePlantRecordMapper;

    /**
     * 查询农事记录信息
     * 
     * @param recordId 农事记录信息ID
     * @return 农事记录信息
     */
    @Override
    public TracePlantRecord selectTracePlantRecordById(Long recordId)
    {
        QueryWrapper<TracePlantRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("record_id",recordId);
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
        }
        return tracePlantRecordMapper.selectOne(queryWrapper);
    }

    /**
     * 查询农事记录信息列表
     * 
     * @param tracePlantRecord 农事记录信息
     * @return 农事记录信息
     */
    @Override
    public List<TracePlantRecord> selectTracePlantRecordList(TracePlantRecord tracePlantRecord)
    {
        try{  //解决未传token的问题
            if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                tracePlantRecord.setDepartId(String.valueOf(SecurityUtils.getUserDepartId()));
            }
        }catch (Exception e) {
            e.printStackTrace();
        }

        return tracePlantRecordMapper.selectTracePlantRecordList(tracePlantRecord);
    }

    /**
     * 新增农事记录信息
     * 
     * @param tracePlantRecord 农事记录信息
     * @return 结果
     */
    @Override
    public boolean insertTracePlantRecord(TracePlantRecord tracePlantRecord)
    {
        tracePlantRecord.setCreateTime(DateUtils.getNowDate());
        tracePlantRecord.setUpdateTime(DateUtils.getNowDate());
        tracePlantRecord.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        tracePlantRecord.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        tracePlantRecord.setDepartId(String.valueOf(SecurityUtils.getUserDepartId()));
        //return tracePlantRecordMapper.insertTracePlantRecord(tracePlantRecord);
        return super.save(tracePlantRecord);
    }

    /**
     * 修改农事记录信息
     * 
     * @param tracePlantRecord 农事记录信息
     * @return 结果
     */
    @Override
    public int updateTracePlantRecord(TracePlantRecord tracePlantRecord)
    {
        tracePlantRecord.setUpdateTime(DateUtils.getNowDate());
        tracePlantRecord.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        return tracePlantRecordMapper.updateById(tracePlantRecord);
    }

    /**
     * 批量删除农事记录信息
     * 
     * @param recordIds 需要删除的农事记录信息ID
     * @return 结果
     */
    @Override
    public int deleteTracePlantRecordByIds(Long[] recordIds)
    {
        QueryWrapper<TracePlantRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("record_id", Arrays.asList(recordIds));
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
        }
        return tracePlantRecordMapper.delete(queryWrapper);
    }

    /**
     * 删除农事记录信息信息
     * 
     * @param recordId 农事记录信息ID
     * @return 结果
     */
    @Override
    public int deleteTracePlantRecordById(Long recordId)
    {
        QueryWrapper<TracePlantRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("record_id", recordId);
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
        }
        return tracePlantRecordMapper.delete(queryWrapper);
    }
}
